package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.PaymentProof;
import com.amsp.oa.oademo.entity.PaymentProofItem;
import com.amsp.oa.oademo.mapper.PaymentProofMapper;
import com.amsp.oa.oademo.mapper.PaymentProofItemMapper;
import com.amsp.oa.oademo.service.PaymentProofService;
import com.amsp.oa.oademo.vo.PaymentProofItemVO;
import com.amsp.oa.oademo.vo.PaymentProofVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PaymentProofServiceImpl implements PaymentProofService {

    @Resource
    private PaymentProofMapper paymentProofMapper;

    @Resource
    private PaymentProofItemMapper paymentProofItemMapper;

    @Override
    public IPage<PaymentProofVO> getList(Page<PaymentProofVO> page, String monthDate) {
        Page<PaymentProof> proofPage = new Page<>(page.getCurrent(), page.getSize());
        LambdaQueryWrapper<PaymentProof> wrapper = new LambdaQueryWrapper<>();
        if (monthDate != null && !monthDate.isEmpty()) {
            String[] parts = monthDate.split("-");
            wrapper.eq(PaymentProof::getStatisticsYear, Integer.parseInt(parts[0]))
                    .eq(PaymentProof::getStatisticsMonth, Integer.parseInt(parts[1]));
        }
        wrapper.orderByDesc(PaymentProof::getCreateTime);

        IPage<PaymentProof> proofIPage = paymentProofMapper.selectPage(proofPage, wrapper);

        return proofIPage.convert(proof -> {
            PaymentProofVO vo = new PaymentProofVO();
            BeanUtils.copyProperties(proof, vo);

            // 查询明细表数据
            LambdaQueryWrapper<PaymentProofItem> itemWrapper = new LambdaQueryWrapper<>();
            itemWrapper.eq(PaymentProofItem::getProofId, proof.getId());
            List<PaymentProofItem> items = paymentProofItemMapper.selectList(itemWrapper);

            // 转换为VO对象
            List<PaymentProofItemVO> itemVOs = items.stream().map(item -> {
                PaymentProofItemVO itemVO = new PaymentProofItemVO();
                BeanUtils.copyProperties(item, itemVO);
                return itemVO;
            }).collect(Collectors.toList());

            vo.setItemList(itemVOs);

            return vo;
        });
    }

    @Override
    public PaymentProofVO getDetail(Long id) {
        PaymentProof proof = paymentProofMapper.selectById(id);
        if (proof == null) {
            return null;
        }

        PaymentProofVO vo = new PaymentProofVO();
        BeanUtils.copyProperties(proof, vo);

        LambdaQueryWrapper<PaymentProofItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentProofItem::getProofId, id);
        List<PaymentProofItem> items = paymentProofItemMapper.selectList(wrapper);

        vo.setItemList(items.stream().map(item -> {
            PaymentProofItemVO itemVO = new PaymentProofItemVO();
            BeanUtils.copyProperties(item, itemVO);
            return itemVO;
        }).collect(Collectors.toList()));

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveOrUpdate(PaymentProofVO vo) {
        PaymentProof proof = new PaymentProof();
        BeanUtils.copyProperties(vo, proof);

        if (vo.getId() == null) {
            paymentProofMapper.insert(proof);
        } else {
            paymentProofMapper.updateById(proof);
            // 删除原有明细
            LambdaQueryWrapper<PaymentProofItem> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PaymentProofItem::getProofId, vo.getId());
            paymentProofItemMapper.delete(wrapper);
        }

        // 保存新的明细
        if (vo.getItemList() != null && !vo.getItemList().isEmpty()) {
            List<PaymentProofItem> items = vo.getItemList().stream().map(itemVO -> {
                PaymentProofItem item = new PaymentProofItem();
                BeanUtils.copyProperties(itemVO, item);
                item.setProofId(proof.getId());
                return item;
            }).collect(Collectors.toList());

            items.forEach(paymentProofItemMapper::insert);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete2(Long id) {
        paymentProofMapper.deleteById(id);

        LambdaQueryWrapper<PaymentProofItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentProofItem::getProofId, id);
        paymentProofItemMapper.delete(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete2(List<Long> ids) {
        paymentProofMapper.deleteBatchIds(ids);

        LambdaQueryWrapper<PaymentProofItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PaymentProofItem::getProofId, ids);
        paymentProofItemMapper.delete(wrapper);
    }
}